package cn.fz.luck.saas.service.serviceImpl;

import cn.fz.luck.saas.mapper.SysPermissionMapper;
import cn.fz.luck.saas.mapper.SysPermissionRoleMapper;
import cn.fz.luck.saas.model.SysPermission;
import cn.fz.luck.saas.model.SysPermissionRole;
import cn.fz.luck.saas.model.SysRole;
import cn.fz.luck.saas.service.SysPermissionService;
import cn.fz.luck.saas.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhuangqi
 * @date 2017/7/6.
 */
@Service
public class SysPermissionServiceImpl implements SysPermissionService{

    @Autowired
    private SysRoleService sysRoleService;

    @Autowired
    private SysPermissionRoleMapper sysPermissionRoleMapper;

    @Autowired
    private SysPermissionMapper sysPermissionMapper;

    @Override
    public List<SysPermission> listByUserId(String userId) {
        List<SysRole> roles = sysRoleService.listByUserId(userId);
        String roleIds = roles.stream().map(SysRole::getRoleId).collect(Collectors.joining(","));
        List<SysPermissionRole> permissionRoles = sysPermissionRoleMapper.selectByIds(roleIds);
        String permissionIds = permissionRoles.stream().map(SysPermissionRole::getRoleId).collect(Collectors.joining(","));
        return sysPermissionMapper.selectByIds(permissionIds);
    }

}
